package xw.qxypt.modules.baseinfo.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xw.qxypt.common.utils.PageUtils;
import xw.qxypt.common.utils.Query;

import xw.qxypt.common.utils.R;
import xw.qxypt.modules.baseinfo.dao.LhpDataRoadDetectorDao;
import xw.qxypt.modules.baseinfo.entity.LhpDataRoadDetectorEntity;
import xw.qxypt.modules.baseinfo.entity.LhpDataVisibilityEntity;
import xw.qxypt.modules.baseinfo.entity.LhpInstallationPointEntity;
import xw.qxypt.modules.baseinfo.entity.LhpThresholdSettingEntity;
import xw.qxypt.modules.baseinfo.service.LhpDataRoadDetectorService;
import xw.qxypt.modules.baseinfo.service.LhpInstallationPointService;
import xw.qxypt.modules.sys.shiro.ShiroUtils;


@Service("lhpDataRoadDetectorService")
public class LhpDataRoadDetectorServiceImpl extends ServiceImpl<LhpDataRoadDetectorDao, LhpDataRoadDetectorEntity> implements LhpDataRoadDetectorService {
    @Autowired
    LhpInstallationPointService lhpInstallationPointService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String pointName = (String)params.get("pointName");
        String startTime = (String)params.get("startTime");
        if(!StringUtils.isNotBlank(startTime)){
            startTime = sdf.format(new Date())+" 00:00:00";
        }
        String endTime = (String)params.get("endTime");
        if(StringUtils.isNotBlank(endTime)){
            endTime = endTime+" 23:59:59";
        }
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
        }
        if(pointName != null && !"".equals(pointName)){
            pIdList = new ArrayList<>();
            for(LhpInstallationPointEntity p:pointList){
                if(p.getPointName().indexOf(pointName) != -1){
                    pIdList.add(p.getId());
                }
            }
        }
        if(pIdList.size()==0){
            pIdList.add("safsedfgdgfdgfdg");
        }
        IPage<LhpDataRoadDetectorEntity> page = this.page(
                new Query<LhpDataRoadDetectorEntity>().getPage(params),
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .in(pIdList.size()>0,"point_id",pIdList)
                        .ge(StringUtils.isNotBlank(startTime),"data_time",startTime)
                        .le(StringUtils.isNotBlank(endTime),"data_time",endTime)
                        .orderByDesc("data_time")
        );
        List<LhpDataRoadDetectorEntity> infoList = page.getRecords();
        for(LhpDataRoadDetectorEntity ldrd:infoList){
            for(LhpInstallationPointEntity lipe:pointList){
                if(ldrd.getPointId().equals(lipe.getId())){
                    ldrd.setPointName(lipe.getPointName());
                    break;
                }
            }
        }
        return new PageUtils(page);
    }

    @Override
    public Map roadChart() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -10);
        //根据用户id查询关联的安装点
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        List<String> pNameList = new ArrayList<>();
        List<String> tempList = new ArrayList<>();
        List<String> fricationList = new ArrayList<>();
        List<String> waterFilmHeightList = new ArrayList<>();
        Map<String,LhpDataRoadDetectorEntity> map1 = new HashMap<>();
        Map<String,LhpInstallationPointEntity> map2 = new HashMap<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
            map2.put(p.getId(), p);
        }

        List<LhpDataRoadDetectorEntity> ldrdList=this.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                .gt("data_time",c.getTime())
                .in("point_id",pIdList)
        );
        LhpDataRoadDetectorEntity minRaodTemp = new LhpDataRoadDetectorEntity();
        LhpDataRoadDetectorEntity minWaterFilmHeight = new LhpDataRoadDetectorEntity();
        LhpDataRoadDetectorEntity minFriction = new LhpDataRoadDetectorEntity();
        if(ldrdList.size()>0) {
            //查询极值
            Collections.sort(ldrdList, new Comparator<LhpDataRoadDetectorEntity>() {
                @Override
                public int compare(LhpDataRoadDetectorEntity o1, LhpDataRoadDetectorEntity o2) {
                    if(Double.parseDouble(o1.getRoadSurfaceTemp()) >= Double.parseDouble(o2.getRoadSurfaceTemp())) {
                        return 1;
                    }else {
                        return -1;
                    }
                }
            });
            minRaodTemp = ldrdList.get(0);
            Collections.sort(ldrdList, new Comparator<LhpDataRoadDetectorEntity>() {
                @Override
                public int compare(LhpDataRoadDetectorEntity o1, LhpDataRoadDetectorEntity o2) {
                    if(Double.parseDouble(o1.getFriction()) >= Double.parseDouble(o2.getFriction())) {
                        return 1;
                    }else {
                        return -1;
                    }
                }
            });
            minFriction = ldrdList.get(0);
            Collections.sort(ldrdList, new Comparator<LhpDataRoadDetectorEntity>() {
                @Override
                public int compare(LhpDataRoadDetectorEntity o1, LhpDataRoadDetectorEntity o2) {
                    if(Double.parseDouble(o1.getWaterFilmHeight()) >= Double.parseDouble(o2.getWaterFilmHeight())) {
                        return -1;
                    }else {
                        return 1;
                    }
                }
            });
            minWaterFilmHeight = ldrdList.get(0);

            for (LhpDataRoadDetectorEntity lhpd : ldrdList) {
                map1.put(lhpd.getPointId(), lhpd);
            }
            for (String  s : pIdList) {
                if(map1.get(s) != null) {
                    tempList.add(map1.get(s).getRoadSurfaceTemp());
                    fricationList.add(map1.get(s).getFriction());
                    waterFilmHeightList.add(map1.get(s).getWaterFilmHeight());
                    pNameList.add(map2.get(s).getPointName());
                }
            }
        }
        Map map=new HashMap();
        map.put("pNameList",pNameList);
        map.put("tempList", tempList);
        map.put("fricationList", fricationList);
        map.put("waterFilmHeightList", waterFilmHeightList);
        map.put("minTemp", minRaodTemp);
        map.put("minFriction", minFriction);
        map.put("minWaterFilmHeight", minWaterFilmHeight);
        return map;
    }

    @Override
    public List<LhpDataRoadDetectorEntity> roadDetectorOverlay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -6);
        //根据用户id查询关联的安装点
        List<LhpInstallationPointEntity> pointList = lhpInstallationPointService.getPointByUserId(ShiroUtils.getUserId());
        List<String> pIdList = new ArrayList<>();
        for(LhpInstallationPointEntity p:pointList){
            pIdList.add(p.getId());
        }

        List<LhpDataRoadDetectorEntity> ldrdList=this.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .gt("data_time",c.getTime())
                        .in("point_id",pIdList)
        );
        return ldrdList;
    }

    @Override
    public LhpDataRoadDetectorEntity getByPointId(String pointId) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -15);
        List<LhpDataRoadDetectorEntity> ldrdList=this.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .gt("data_time",c.getTime())
                        .eq("point_id",pointId)
                        .orderByDesc("data_time")
        );
        if(ldrdList.size()>0){
            LhpDataRoadDetectorEntity roadDetector=ldrdList.get(0);
            if(roadDetector.getFriction() != "" && roadDetector.getFriction() != null){
                Double friction=Double.parseDouble(roadDetector.getFriction());
                if(friction>0.64){
                    roadDetector.setFriction("良好");
                }else if(friction>0.55){
                    roadDetector.setFriction("正常");
                }else if(friction>0.50){
                    roadDetector.setFriction("稍差");
                }else if(friction>0.40){
                    roadDetector.setFriction("较差");
                }else if(friction>0.30){
                    roadDetector.setFriction("很差");
                }else{
                    roadDetector.setFriction("极差");
                }
            }
            if(roadDetector.getRoadCondition() !="" && roadDetector.getRoadCondition() != null) {
                if ("10".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("干");
                } else if ("15".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮");
                } else if ("20".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿");
                } else if ("25".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮含盐");
                } else if ("30".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿含盐");
                } else if ("35".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰");
                } else if ("40".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("雪");
                } else if ("45".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("霜");
                }else if ("1".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("干");
                }else if ("2".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮");
                }else if ("3".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿");
                }else if ("6".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("雪");
                }else if ("7".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰");
                }else if ("9".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰水混合");
                }
            }
            if(roadDetector.getFreezingTempNacl() == null){
                roadDetector.setFreezingTempNacl("-0.1");
            }
            if(roadDetector.getIcePercentage() == null){
                roadDetector.setIcePercentage("0.00");
            }
            return roadDetector;
        }
        return null;
    }

    @Override
    public LhpDataRoadDetectorEntity getDataRoadDetectorByPointId(String pointId) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, -15);
        List<LhpDataRoadDetectorEntity> ldrdList=this.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .gt("data_time",c.getTime())
                        .eq("point_id",pointId)
                        .orderByDesc("data_time")
        );
        if(ldrdList.size()>0){
            LhpDataRoadDetectorEntity roadDetector=ldrdList.get(0);
            if(roadDetector.getRoadCondition() !="" && roadDetector.getRoadCondition() != null) {
                if ("10".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("干");
                } else if ("15".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮");
                } else if ("20".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿");
                } else if ("25".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮含盐");
                } else if ("30".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿含盐");
                } else if ("35".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰");
                } else if ("40".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("雪");
                } else if ("45".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("霜");
                }else if ("1".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("干");
                }else if ("2".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("潮");
                }else if ("3".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("湿");
                }else if ("6".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("雪");
                }else if ("7".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰");
                }else if ("9".equals(roadDetector.getRoadCondition())) {
                    roadDetector.setRoadCondition("冰水混合");
                }
            }
            return roadDetector;
        }
        return null;
    }

    @Override
    public List<LhpDataRoadDetectorEntity> roadDetectorChart(String pointId) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.HOUR, -3);
        List<LhpDataRoadDetectorEntity> ldrdList=this.list(
                new QueryWrapper<LhpDataRoadDetectorEntity>()
                        .gt("data_time",c.getTime())
                        .eq("point_id",pointId)
                        .orderByAsc("data_time")
        );
        return ldrdList;
    }

    @Override
    public List<LhpDataRoadDetectorEntity> queryRoadDataByRoleId(Date startTime, String roleId) {
        return this.baseMapper.queryRoadDataByRoleId(startTime,roleId);
    }

}
